package cn.wolfcode.controller;

import cn.wolfcode.domain.Classinfo;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.ClassinfoQueryObject;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IClassinfoService;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/classinfo")
public class ClassinfoController {

    @Autowired
    private IClassinfoService classinfoService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IRoleService roleService;

    //查询列表
    @RequestMapping("/list")
    //@ModelAttribute高级查询用回显
    public String list(Model model, @ModelAttribute("qo") ClassinfoQueryObject qo){
        PageInfo<Classinfo> query = classinfoService.query(qo);
        model.addAttribute("pageInfo",classinfoService.query(qo));
        model.addAttribute("depts",employeeService.listAll());
        return "/classinfo/list";
    }

    //删除
    @RequestMapping("/delete")
    public String delete(Long id){
        classinfoService.delete(id);
        return "redirect:/classinfo/list.do";
    }

    /*//添加和修改跳转页面
    @RequestMapping("/input")
    public String input(Model model,Long id){
        model.addAttribute("employees",employeeService.listAll());
        if (id != null) {
            Classinfo classinfo = classinfoService.get(id);
            model.addAttribute("classinfo",classinfo);
        }
        List<Role> roles = roleService.listAll();
        model.addAttribute("roles",roles);
        return "/classinfo/input";
    }*/

    //添加和修改
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Classinfo classinfo,Long[] ids){
        if (classinfo.getId() != null) {
            classinfoService.update(classinfo,ids);
        }else{
            //添加
            classinfoService.save(classinfo,ids);
        }
        return new JsonResult();
    }

    /*//验证用户名是否存在
    @RequestMapping("/checkName")
    @ResponseBody
    public Map<String,Boolean> checkName(String name,Long id){
        //通过判断id是否有值,如果有值,是编辑,没有值,是保存,
        HashMap<String,Boolean> map = new HashMap<>();
        if(id!=null){
            //编辑
            //根据id查询名称是否一致,是的话继续验证,不是的话中断
            Classinfo classinfo = classinfoService.get(id);
            map.put("valid",classinfo.getName().equals(name));
            return map;
        }else{
            //保存,通过名字查找,如果是空的,就继续,不是空的就中断
            Classinfo classinfo = classinfoService.selectByName(name);
            map.put("valid",classinfo==null);
            return map;
        }
    }*/
}
